package com.ebeitech.library.util;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by lori.lin
 * on 7/21/2015 3:59 PM.
 * usage:窗口管理工具
 */
public class WindowsUtil {

    private static Context context;


    public static WindowsUtil windowsUtil;

    public WindowsUtil(Context context)
    {
        this.context = context;
    }

    /**
     * 生成方法
     * @return
     */
    public static WindowsUtil getInstance(Context context)
    {
        if (windowsUtil==null)
        {
            windowsUtil = new WindowsUtil(context);
        }
        return windowsUtil;
    }

    /**
     * 获取window宽度
     * @return
     */
    public int getWindowX()
    {
        return getWindowDisplay().widthPixels;
    }

    /**
     * 获取windows高度
     * @return
     */
    public int getWindowY()
    {
        return getWindowDisplay().heightPixels;
    }

    /**
     * 生成窗口Metrics
     * @return
     */
    public DisplayMetrics getWindowDisplay()
    {
        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        return metrics;
    }

    public int getViewWidth(View view)
    {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getWidth();
    }

    public int getViewHeight(View view)
    {
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        view.measure(w,h);
        return h;
    }

    public void setScrollTop(View view)
    {
        /**
         * 保证滚动到顶部
         */
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
    }
}
